/**
 * 本地调用h5中的js
 */

package com.aofeng.hybrid.plugin;

import android.app.Activity;
import android.webkit.WebView;

import com.aofeng.hybrid.util.LogUtil;

import org.json.JSONObject;

import java.lang.ref.WeakReference;

/**
 *  本地调用js函数
 */
public class NativeCallJS {
    private static final String CALLBACK_JS_FORMAT = "javascript:%s.callback(%d, %d %s);";
    private int mIndex;
    private boolean mCouldCallAgain;
    private WeakReference<WebView> mWebViewRef;
    private int mIsSticky;
    private String mInjectedName;

    public NativeCallJS (WebView view, String injectedName, int index) {
        mCouldCallAgain = true;
        mWebViewRef = new WeakReference<WebView>(view);
        mInjectedName = injectedName;
        mIndex = index;
    }

    public void apply (Object ... args) throws JsCallbackException {
        if (mWebViewRef.get() == null) {
            throw new JsCallbackException("弱引用失效。");
        }
        if (!mCouldCallAgain) {
            throw new JsCallbackException("不能多次调用。");
        }
        StringBuilder sb = new StringBuilder();
        for (Object arg : args){
            sb.append(",");
            boolean isStrArg = arg instanceof String;
            if (isStrArg) {
                sb.append("\"");
            }
            if(arg instanceof JSONObject)
                sb.append(JSONObject.quote(((JSONObject)arg).toString()));
            else
                sb.append(String.valueOf(arg));
            if (isStrArg) {
                sb.append("\"");
            }
        }
        final String execJs = String.format(CALLBACK_JS_FORMAT, mInjectedName, mIndex, mIsSticky, sb.toString());
        LogUtil.d("JsCallBack", execJs);
        mCouldCallAgain = mIsSticky > 0;
        ((Activity)mWebViewRef.get().getContext()).runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mWebViewRef.get().loadUrl(execJs);
            }
        });
    }

    public void setPermanent (boolean value) {
        mIsSticky = value ? 1 : 0;
    }

    public static class JsCallbackException extends Exception {
		private static final long serialVersionUID = 1L;

		public JsCallbackException (String msg) {
            super(msg);
        }
    }
}
